<?php
	
	$user = $controller->getCurrentUser();
	//on verifie les permissions
	if ($user->isAllowedTo(GRADE_MOD_BOOK))
	{
		$type = $request->getParam('typeElt');
		$idElement = $request->getParam('idElement');
		
		if (!$type || !in_array($type,array('serie','publisher','author')))
			throw new exceptionErrorManual(0,'Mauvais type passe en post (ou absent)',__file__,__line__);
		if (!$idElement || !is_numeric($idElement))
			throw new exceptionErrorManual(0,'idElement mal passe en post',__file__,__line__);
		
		if ($type=='serie')
		{
			$name = $request->getParam('nameElement');
			$genre = $request->getParam('genreSerie');
			$type = $request->getParam('typeSerie');
			$stateBDBDEC = $request->getParam('stateBDBDEC');
			$statePublisher = $request->getParam('statePublisher');
			$keywords = $request->getParam('keywordsSerie');
			
			
			if (!$name || !$genre || !$type || !$stateBDBDEC || !$statePublisher)
			{
				$controller->addError('Vous devez remplir les champs n&eacute;cessaires');
				$controller->forward('manageAuthorsSeries');
			}
			else
			{
				$serie = serie::singleton($idElement);
				if (!$serie->getId())
					throw new exceptionErrorManual(0,'Serie non cree',__file__,__line__);
				
				$serie->setGenre($genre);
				$serie->setTypeSerie($type);
				if ($keywords)
					$serie->setKeywords($keywords);
				else
					$serie->delKeywords();
				$serie->setName($name);
				$serie->setStateBDBDEC($stateBDBDEC);
				$serie->setStatePublisher($statePublisher);
				
				if ($serie->readyToAdd())
				{
					$serie->update();
					$logPermAction = 'S&eacute;rie mise &agrave; jour : '.addslashes($serie->getName());
					logPermactions($user->getId(), $logPermAction); 
					$controller->addInformation('La s&eacute;rie a &eacute;t&eacute; mise &agrave; jour !');
					$controller->forward('manageAuthorsSeries');
				}
				else
				{
					$controller->addError('Remplissez les champs du formulaire correctement');
					$controller->render('manageAuthorsSeries');
				}
			}
		}
		else 
		{
			
			$name = $request->getParam('nameElement');
			
			if (!$name)
			{
				$controller->addError('Vous devez remplir les champs n&eacute;cessaires');
				$controller->forward('manageAuthorsSeries');
			}
			else
			{
				$element = call_user_func(array($type, 'singleton'),$idElement);
				if (!$element->getId())	
					throw new exceptionErrorManual(0,$type.' non cree',__file__,__line__);
					
				$element->setName($name);
				if ($element->readyToAdd())
				{
					$element->update();
					$logPermAction = $type.' mis &agrave; jour : '.addslashes($element->getName());
					logPermactions($user->getId(), $logPermAction); 
					$controller->addInformation('L\'&eacute;l&eacute;ment a &eacute;t&eacute; mis &agrave; jour !');
					$controller->forward('manageAuthorsSeries');
				}
				else
				{
					$controller->addError('Remplissez les champs du formulaire correctement');
					$controller->render('manageAuthorsSeries');
				}
			}	
		}
	}
	else
		$controller->render('errorRights');
?>
